﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace DEEBCG.ExternalLib.CompanyAPI.Model
{
    /// <summary>
    /// 日报表信息
    /// </summary>
    public class DayReportInfo
    {

        /// <summary>用户编号</summary>
        public string UserID { get; set; } = "";

        /// <summary>设备编号</summary>
        public string DeviceID { get; set; } = "";

        /// <summary>
        /// 报表编号
        /// </summary>
        public int ReportID { set; get; }

        /// <summary>报表日期</summary>
        public DateTime ReportDate
        {
            get;
            set;
        }

        /// <summary>
        /// 睡眠评级
        /// </summary>
        [JsonIgnore]
        public string SleepLevel
        {
            set; get;
        }

        /// <summary>
        /// 深睡比例
        /// </summary>
        [JsonIgnore]
        public double DeepSleepScale
        {
            set;
            get;
        }

        /// <summary>
        /// 是否完成日报表
        /// </summary>
        [JsonIgnore]
        public bool IsFinishReport { get; set; } = false;

        /// <summary>
        /// 报表描述
        /// </summary>
        public string ReportDesc { get; set; } = "";

        /// <summary>上床时间</summary>
        public DateTime StartOnBedTime
        {
            get;
            set;
        }
        /// <summary>起床时间</summary>
        public DateTime EndOnBedTime
        {
            get;
            set;
        }
        /// <summary>入睡时间</summary>
        public DateTime StartSleepTime
        {
            get;
            set;
        }
        /// <summary>醒来时间</summary>
        public DateTime EndSleepTime
        {
            get;
            set;
        }
        /// <summary>在床时长</summary>
        public double TotalOnBedHours
        {
            get;
            set;
        }
        /// <summary>睡眠时长</summary>
        public double TotalSleepHours
        {
            get;
            set;
        }
        /// <summary>睡眠活动次数</summary>
        public int SleepActityCount
        {
            get;
            set;
        }
        /// <summary>睡眠平均心率</summary>
        public double SleepAvgHeartRate
        {
            get;
            set;
        }

        /// <summary>
        /// 睡眠心率变异度
        /// </summary>
        public double SDNN
        {
            get;
            set;
        }

        /// <summary>
        /// 睡眠呼吸变异度
        /// </summary>
        public double CVBR
        {
            get;
            set;
        }

        /// <summary>
        /// 晨脉(醒来时)
        /// </summary>
        public double MorningVeins1
        {
            get;
            set;
        }

        /// <summary>
        /// 晨脉(醒来前)
        /// </summary>
        public double MorningVeins2
        {
            get;
            set;
        }

        /// <summary>睡眠平均呼吸</summary>
        public double SleepAvgBreatheRate
        {
            get;
            set;
        }

        /// <summary>睡眠平均室温</summary>
        public double SleepAvgTemperature
        {
            get;
            set;
        }
        /// <summary>起夜次数</summary>
        public int OutBedInSleepCount
        {
            get;
            set;
        }
        /// <summary>起夜总时长</summary>
        public double TotalOutBedInSleepMinute
        {
            get;
            set;
        }

        /// <summary>是否有午休</summary>
        [JsonIgnore]
        public bool HasSiesta
        {
            get;
            set;
        }
        /// <summary>午休次数</summary>
        [JsonIgnore]
        public int SiestaCount
        {
            get;
            set;
        }

        /// <summary>午休总时长</summary>
        [JsonIgnore]
        public double TotalSiestaHours
        {
            get;
            set;
        }

        /// <summary>
        /// 平均吸气呼气比例
        /// </summary>
        public double AvgInhaleExhaleScale
        {
            set;
            get;
        }


        /// <summary>
        /// 平均血压
        /// </summary>
        public double SleepAvgBloodPressure
        {
            set;
            get;
        }

        /// <summary>
        /// 是否有打鼾情况（0无）
        /// </summary>
        public int HasSnore
        {
            set;
            get;
        }

        /// <summary>
        /// 平均打鼾频率
        /// </summary>
        public double SleepAvgSnoreRate
        {
            set;
            get;
        }

        /// <summary>
        /// 睡眠发生低通的次数
        /// </summary>
        public int SleepApneaCount
        {
            get;
            set;
        } = 0;

        /// <summary>
        /// 平均呼吸低通时长
        /// </summary>
        public double AvgApneaInterval
        {
            get;
            set;
        } = 0;

        /// <summary>
        /// 最长低通时长
        /// </summary>
        public double MaxApeaInterval
        {
            get;
            set;
        } = 0;

        /// <summary>
        /// 最长低通发生时间
        /// </summary>
        public DateTime MaxApneaHappendTime
        {
            set;
            get;
        }

        /// <summary>
        /// 睡眠时起夜信息列表
        /// </summary>
        public List<DayNightLeaveBedReportInfo> NightLeaveBedList { set; get; } = new List<DayNightLeaveBedReportInfo>();

        /// <summary>
        /// 晚上睡眠区间
        /// </summary>
        [JsonIgnore]
        public List<DayNightSleepReportInfo> DayNightSleepList { set; get; } = new List<DayNightSleepReportInfo>();

        /// <summary>
        /// 异常代码
        /// </summary>
        public int UnusualCode
        {
            set;
            get;
        }

        /// <summary>
        /// 综合评价
        /// </summary>
        public string Overview
        {
            set;
            get;
        }

        /// <summary>
        /// 睡眠评价
        /// </summary>
        public string SleepOverview
        {
            set;
            get;
        }

        /// <summary>
        /// 心跳评价
        /// </summary>
        public string HeartOverview
        {
            set;
            get;
        }

        /// <summary>
        /// 呼吸评价
        /// </summary>
        public string BreatheOverview
        {
            set;
            get;
        }

        /// <summary>
        /// 打鼾评价
        /// </summary>
        public string SnoreOverview
        {
            set;
            get;
        }

        /// <summary>
        /// 起夜评价
        /// </summary>
        public string NightLeaveBedOverview
        {
            set;
            get;
        }



    }

    public class DayNightSleepReportInfo
    {

        public DayNightSleepReportInfo()
        {
            IsPrimary = false;
        }

        public DayNightSleepReportInfo(DateTime start, DateTime end)
        {
            SleepTime = start;
            WakeUpTime = end;
        }


        /// <summary>
        /// 入睡时间
        /// </summary>
        public DateTime SleepTime { set; get; }
        /// <summary>
        /// 醒来时间
        /// </summary>
        public DateTime WakeUpTime { set; get; }

        /// <summary>
        /// 是否为主要的睡眠时间
        /// </summary>
        public bool IsPrimary { set; get; } = false;

        /// <summary>
        /// 睡眠时长
        /// </summary>
        public double TotalSleepMinutes
        {
            get
            {
                if (WakeUpTime == default(DateTime) || WakeUpTime == default(DateTime))
                    return 0;
                else
                    return (WakeUpTime - SleepTime).TotalMinutes;
            }
        }

        /// <summary>
        /// 睡眠时长
        /// </summary>
        public double TotalSleepHours
        {
            get
            {
                if (WakeUpTime == default(DateTime) || WakeUpTime == default(DateTime))
                    return 0;
                else
                    return (WakeUpTime - SleepTime).TotalHours;
            }
        }
    }

    /// <summary>
    /// 睡眠时起夜时间
    /// </summary>
    public class DayNightLeaveBedReportInfo
    {

        /// <summary>
        /// 醒来时间
        /// </summary>
        public DateTime WakeUpTime
        {
            get; set;
        }
        /// <summary>
        /// 起床时间
        /// </summary>
        public DateTime OutBedTime
        {
            get; set;
        }
        /// <summary>
        /// 上床时间
        /// </summary>
        public DateTime BackOnBedTime
        {
            get; set;
        }
        /// <summary>
        /// 总离床时间
        /// </summary>
        public double TotalOutBedSecond
        {
            get
            {
                try
                {
                    return (BackOnBedTime - OutBedTime).TotalSeconds;
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("起床时间{0},上床时间{1},总离床时间{2}秒", OutBedTime, BackOnBedTime, TotalOutBedSecond);
        }
    }
}
